package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.house.biz.BillPayBiz;
import guda.house.biz.beans.vo.BillPayXlsVO;
import guda.house.biz.beans.vo.NoticeBillVO;
import guda.house.biz.form.bill.BatchNoticeBillForm;
import guda.house.biz.form.bill.ConfirmPayForm;
import guda.house.biz.form.bill.NoticeBillForm;
import guda.house.biz.form.bill.UnpaySearchForm;
import guda.house.biz.query.BillPayQuery;
import guda.house.biz.security.AppUtil;
import guda.house.biz.util.ExportExcelUtil;
import guda.house.biz.util.PriceUtil;
import guda.house.dal.dataobject.BillPayDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by well on 15/11/20.
 */
@Controller
@RequestMapping(value = "bill/unpay")
public class NoticeBillAction {

    @Autowired
    private BillPayBiz billPayBiz;

    @RequestMapping(value = "noticeBill.htm",method = RequestMethod.POST )
    public String noticeBill(HttpServletRequest request, ModelMap modelMap, @Valid UnpaySearchForm unpaySearchForm, BindingResult result) {

        if(result.hasErrors()){
            return "bill/unpay/list.vm";
        }

        BizResult bizResult = billPayBiz.initForPrint(unpaySearchForm);
        modelMap.putAll(bizResult.data);

        return "bill/unpay/noticeBill.vm";
    }



    @RequestMapping(value = "batchNoticeBill.htm")
    public String batchNoticeBill(HttpServletRequest request, ModelMap modelMap,  BatchNoticeBillForm batchNoticeBillForm, BindingResult result) {

        if(batchNoticeBillForm.getVillageId() == null) {
            List<Long> villageIdList = AppUtil.getVillageIdList();
            if (villageIdList.size() > 0) {
                batchNoticeBillForm.setVillageId(villageIdList.get(0));
            }
        }


        BizResult bizResult = billPayBiz.initForPrintBatch(batchNoticeBillForm);
        modelMap.putAll(bizResult.data);

        return "bill/unpay/batchNoticeBill.vm";
    }

    @RequestMapping(value = "exportNoticeBill.do",method = RequestMethod.GET)
    public void export(HttpServletResponse response, ModelMap modelMap,  BatchNoticeBillForm batchNoticeBillForm, BindingResult result) {

        String filedisplay = null;
        try {
            filedisplay = URLEncoder.encode("催费单.xls", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/x-download; charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment; filename=" + filedisplay);

        BizResult bizResult = billPayBiz.initForPrintBatch(batchNoticeBillForm);
        List<NoticeBillVO> noticeBillVOList = (List<NoticeBillVO>)bizResult.data.get("noticeBillVOList");

        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ExportExcelUtil.exportNoticeBillExcel("催费单", noticeBillVOList, outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
